const { get } = require('../../../utils/request.js')
const EventBus = require('../../../utils/eventBus.js')

Page({
  data: {
    showPassword: false,
    onMoving: false,
    triggered: false,
    hasParents: false,
    hasChild: false,
    hasConsult: false,
    list: [{
      id: "family",
      name: "家庭成员",
      icon:'setting3.png',
      split: false
    }, {
      id: "rate",
      name: "利率",
      icon:'setting8.png',
      split: true
    }, {
      id: "tag",
      name: "标签",
      icon:'setting1.png',
      split: false
    }, {
      id: "password",
      name: "操作密码",
      icon:'setting6.png',
      split: false
    }, {
      id: "backupjob",
      name: "数据导出",
      icon:'setting2.png',
      split: false
    },
    //  {
    //   id: "cost",
    //   name: "续费",
    //   split: false
    // }, 
    {
      id: "help",
      name: "功能解读",
      icon:'setting7.png',
      split: true
    }, {
      id: "produce",
      name: "产品介绍",
      icon:'setting4.png',
      split: false
    }, {
      id: "consult",
      name: "留言咨询",
      icon:'setting5.png',
      split: false
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })

    EventBus.on('setting.checkFamilyInfo', this.checkFamilyInfo)

    this.checkFamilyInfo();

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      "title": `儿童微行`,
      "path": `pages/tabBar/summary/home`,
      "imageUrl": "/resoures/logo.png"
    }
  },
  onShareTimeline() {
    return {
      "title": `儿童微行`,
      "path": `pages/tabBar/summary/home`,
      "imageUrl": "/resoures/logo.png"
    }
  },
  onTouchItem({ currentTarget: { id } }) {
    const list = this.data.list;
    list[id].active = true;
    this.setData({
      list
    })
  },
  onTouchEndItem({ currentTarget: { id } }) {
    const list = this.data.list;
    const targetId = list[id].id;
    list[id].active = false;
    this.setData({
      list
    });

    if (this.data.onMoving) {
      return;
    }

    if (targetId != 'family' && targetId != 'help' && targetId != 'produce'
      && !this.data.hasParents) {
      wx.showToast({
        title: '请设置家庭成员',
        icon: 'error',
        duration: 2000
      });
      return
    }

    if (targetId == 'cost' && !this.data.hasChild) {
      wx.showToast({
        title: '请添加小孩信息',
        icon: 'error',
        duration: 2000
      });
      return
    }

    if (targetId == 'consult' && !this.data.hasConsult) {
      wx.navigateTo({
        url: `/pages/me/consult/consultForm/consultForm`,
      })
    } else if (targetId == 'rate') {
      EventBus.emit('password.showPageContainer', true)
      this.setData({
        showPassword: true
      })
    } else {
      EventBus.emit('password.showPageContainer', false)
      wx.navigateTo({
        url: `/pages/me/${targetId}/${targetId}`,
      })
    }
  },

  checkFamilyInfo() {
    get('family/checkfamily', {
      "unicode": wx.getStorageSync('unicode')
    }, ({ hasExpireCard, hasConsult, hasReplyConsult, hasParents, hasChild }) => {
      setTimeout(() => {
        this.setData({
          triggered: false
        })
      }, 1000);

      // this.data.list.filter(p => p.id == 'cost')[0].showTip = hasExpireCard;

      this.data.list.filter(p => p.id == 'consult')[0].showTip = hasReplyConsult;

      this.setData({
        hasConsult: hasConsult,
        hasParents: hasParents,
        hasChild: hasChild,
        list: this.data.list
      })
    })
  },
  downPullFresh() {
    this.checkFamilyInfo();
  },
  onTouchMove() {
    if (!this.data.onMoving) {
      this.setData({
        onMoving: true
      })
    }
  },
  onTouchMoveEnd() {
    this.setData({
      onMoving: false
    })
  },
  submitRateTo() {
    wx.navigateTo({
      url: `/pages/me/rate/rate`,
    })
  }


})